package com.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.RequestPath;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

/**
 * GatewayFilter
 *
 * @author liujin
 * @date 2020/12/3 22:19
 */
@Slf4j
@Component
public class GatewayFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        /*MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
        List<String> list = queryParams.get("role");
        if (!CollectionUtils.isEmpty(list) && list.contains("admin")) {
            System.out.println("admin，禁止访问");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap("".getBytes(StandardCharsets.UTF_8));
        exchange.getResponse().writeWith(Flux.just(buffer));*/
        log.info("------->");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
